package com.android.zing.api;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.MalformedURLException;
import java.util.HashMap;

import org.apache.http.HttpException;
import org.json.JSONException;
import org.json.JSONObject;

import com.android.zing.ZME_Base;
import com.android.zing.ZME_Connection;
import com.android.zing.ZME_Error;

import android.util.Log;

public class ZME_ASyncGraphAPI {

	ZME_Base zme;

	private static final String TAG = "ZME_ASyncGraphAPI";
	protected final String ME_INFO_PATH = "me";
	protected final String ME_FRIEND_LIST_PATH = "me/friends";
	protected final String ME_FRIEND_INFO_PATH = "user/info";
	protected final String ME_IS_FAN_OF_PATH = "me/isfanof";

	public ZME_ASyncGraphAPI(ZME_Base zme) {
		this.zme = zme;
	}

	protected void request(final String endpoint,
			final HashMap<String, String> params, final RequestListener listener) {
		Log.d(TAG, ">>>> ZME_ASyncGraphAPI :: request");
		new Thread() {
			@Override
			public void run() {
				try {
					Log.d(TAG, "before request to server :: endpoint : "
							+ endpoint + " - params: " + params.toString());
					Object response = ZME_Connection.request(endpoint,
							params);
					Log.d(TAG, "response from server : " + response.toString());
					listener.onComplete(response);
				} catch (NullPointerException e) {
					listener.onNullPointerException(e);
				} catch (FileNotFoundException e) {
					e.printStackTrace();
					listener.onFileNotFoundException(e);
				} catch (MalformedURLException e) {
					e.printStackTrace();
					listener.onMalformedURLException(e);
				} catch (IOException e) {
					e.printStackTrace();
					listener.onIOException(e);
				} catch (HttpException e) {
					e.printStackTrace();
					listener.onHttpException(e);
				} catch (ZME_Error e) {					
					listener.onZmeError(e);
				}
			}
		}.start();
		Log.d(TAG, "<<<< ZME_ASyncGraphAPI :: request");
	}

	/**
	 * Callback interface for API requests.
	 * 
	 * Each method includes a 'state' parameter that identifies the calling
	 * request. It will be set to the value passed when originally calling the
	 * request method, or null if none was passed.
	 */
	public static interface RequestListener {

		/**
		 * Called when a request completes with the given response.
		 * 
		 * Executed by a background thread: do not update the UI in this method.
		 */
		public void onComplete(Object data);

		/**
		 * Called when a request has a network or request error.
		 * 
		 * Executed by a background thread: do not update the UI in this method.
		 */
		public void onIOException(IOException e);

		public void onNullPointerException(NullPointerException e);

		public void onJsonException(JSONException e);

		/**
		 * Called when a request fails because the requested resource is invalid
		 * or does not exist.
		 * 
		 * Executed by a background thread: do not update the UI in this method.
		 */
		public void onFileNotFoundException(FileNotFoundException e);

		/**
		 * Called if an invalid graph path is provided (which may result in a
		 * malformed URL).
		 * 
		 * Executed by a background thread: do not update the UI in this method.
		 */
		public void onMalformedURLException(MalformedURLException e);

		/**
		 * Called when the server-side Facebook method fails.
		 * 
		 * Executed by a background thread: do not update the UI in this method.
		 */
		public void onZmeError(ZME_Error e);

		public void onHttpException(HttpException e);

	}
}
